import { defineConfig, loadEnv, ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { modifyVars } from './build/modifyVars'
import ViteComponents, { AntDesignVueResolver } from 'vite-plugin-components';

const path = require("path");

// https://vitejs.dev/config/ 
//注：在config文件中无法通过import.meta来访问，采用dotenv把env文件加载到process上
export default defineConfig((mode: ConfigEnv) => {
    const env = loadEnv(mode.mode, process.cwd());
    return {
        plugins: [
            vue(),
            // //按需加载
            ViteComponents({
                customComponentResolvers: [AntDesignVueResolver()],
            })
        ],
        optimizeDeps: {
            include: []
        },
        base: env.VITE_BASE_URL,
        resolve: {
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', 'png'],
            alias: {
                '@': path.resolve(__dirname, './src'),
                '~': path.resolve(__dirname, './node_modules')
            }
        },
        server: {
            port: env.VITE_PORT as unknown as number,
            https: false,
            open: true,
            proxy: {
                '/sde': {
                    target: env.VITE_APP_API_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/sde/, ''),
                },
                '/skyApi': {
                    target: env.VITE_APP_API_BASE_URL_2,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/skyApi/, ''),
                },
            },
        },
        build: {
            outDir: env.VITE_OUTPUT_DIR
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars,
                    javascriptEnabled: true,
                }
            },
        },
    }
})